export default {
  namespace: 'users',
  state: {
    cacheAK: 'lmylmy0123456789',
    token: '',
    Info: {},
  },
  reducers: {
    setInfo(state, { payload }) {
      return {
        ...state,
        Info: payload,
      };
    },
    setToken(state, { payload }) {
      return {
        ...state,
        token: payload,
      };
    },
  },
  effects: {
    // *login({ payload }, { call, put }) {
    //   try {
    //     const { code, userinfo } = yield call(loginReq, payload);
    //     localStorage.setItem('userinfo', JSON.stringify(userinfo));
    //     // 触发reducers更新状态
    //     // 犹如import { call, put, takeEvery } from 'redux-saga/effects'中的takeEvery
    //     yield put({
    //       type: 'init',
    //       payload: userinfo,
    //     });
    //     history.push('/');
    //   } catch (error) {
    //     console.log(error);
    //   }
    //   const { data } = yield call(login, payload);
    //   yield put({ type: 'setInfo', payload: data });
    // },
  },
  subscriptions: {
    setup({ dispatch }) {
      const data = sessionStorage.getItem('token');
      if (!data) {
        dispatch({
          type: 'setToken',
          payload: {},
        });
      } else {
        dispatch({
          type: 'setToken',
          payload: data,
        });
      }
    },
  },
};
